package elitech.vietnam.myfashion.extend;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.CopyImageViewActivity;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.entites.Product;
import elitech.vietnam.myfashion.entites.ProductDetail;
import elitech.vietnam.myfashion.view.RecommendGroupView;
import elitech.vietnam.myfashion.widgets.ScaleImageView;
import elitech.vietnam.myfashion.widgets.ScaleImageView.ScaleImageViewEvent;

public class DetailAdapter extends ArrayAdapter<ProductDetail> {
	private MainActivity		mContext;
	// private HeaderDetailView mHeader;
	public RecommendGroupView	mRecommendGroupView;
	public boolean				isRecommend	= true;

	public DetailAdapter(MainActivity context, int resource, int textViewResourceId, Product product) {
		super(context, resource, textViewResourceId, product.DetailList);
		this.mContext = context;
	}

	static class ViewHolder {
		ScaleImageView	image;
		TextView		txtContent;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ProductDetail detail = getItem(position);
		final ViewHolder holder;

		if (row == null) {

			LayoutInflater inflater = mContext.getLayoutInflater();
			row = inflater.inflate(R.layout.photodetail, parent, false);
			holder = new ViewHolder();

			holder.image = (ScaleImageView) row.findViewById(R.id.detail_imgDetail);
			holder.txtContent = (TextView) row.findViewById(R.id.detail_txtDetailContent);

			// holder.image.SetAnimation(true);
			holder.image.setOnDetachedFromWindow(new ScaleImageViewEvent() {
				@Override
				public void onDetachedFromWindow() {
					Log.w("Shopping", "ScaleImageView onDetachedFromWindow");
					Drawable drawable = holder.image.getDrawable();
					if (drawable != null) {
						Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
						if (bm != null && !bm.isRecycled()) {
							bm.recycle();
							bm = null;
							drawable = null;
						}
					}
				}
			});
			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();
		// holder.image.setImageResource(R.drawable.loading300);
		String str = Const.SERVER_IMAGE_URL + detail.Image;
		if (str.indexOf("post") != -1)
			str = str.replace("app/", "");
		if (str.indexOf("account") != -1)
			str = str.replace("app/", "");
//		 new DownloadImageTask(mContext, holder.image, false, 1).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, detail.Image, "/app");
//		Log.w("ImageDetail", str.replace("/app", ""));
		UrlImageViewHelper.setUrlDrawable(holder.image, str.replace("app/", ""));

		if (detail.Content != null && !detail.Content.equals("")) {
			holder.txtContent.setText(detail.Content.replace("<br>", "\n"));
			holder.txtContent.setPadding(8, 8, 8, 8);
		} else
			holder.txtContent.setVisibility(View.GONE);

		holder.image.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(mContext, CopyImageViewActivity.class);
				i.putExtra("Link", detail.Image);
				mContext.startActivity(i);
			}
		});

		return row;
	}

}
